package com.rtp2p.rtcoresdkdemo.add.wifi

import android.content.Intent
import android.provider.Settings
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.viewModels
import com.rtp2p.rtcoresdkdemo.R
import com.rtp2p.rtcoresdkdemo.add.AddViewModel
import com.rtp2p.rtcoresdkdemo.base.BaseFragment
import com.rtp2p.rtcoresdkdemo.databinding.AddConfigWifiFragmentBinding

class AddConfigWifiFragment : BaseFragment() {
    private val addViewModel: AddViewModel by viewModels({ requireActivity() })
    private val viewModel: AddConfigWifiViewModel by viewModels()
    override fun initWorkspaceAction(inflater: LayoutInflater, container: ViewGroup?): View {
        val binding = DataBindingUtil.inflate<AddConfigWifiFragmentBinding>(
            inflater,
            R.layout.add_config_wifi_fragment, container, false
        )
        binding.lifecycleOwner = viewLifecycleOwner
        binding.vm = viewModel

        binding.btnReturn.setOnClickListener { finish() }

        binding.btnSsid.setOnClickListener {
            val intent = Intent(Settings.ACTION_WIFI_SETTINGS)
            startActivity(intent)
        }

        binding.btnNext.setOnClickListener {
            addViewModel.updateWifi(
                viewModel.ssid.value ?: "",
                viewModel.pwd.value ?: ""
            )

            if (AddViewModel.AddType.Ap == addViewModel.addType) {
                startFragment(R.id.action_addConfigWifiFragment_to_addBindingApFragment)
            } else if (AddViewModel.AddType.Ble == addViewModel.addType) {
                startFragment(R.id.action_addConfigWifiFragment_to_addBindingBleFragment)
            }
        }
        return binding.root
    }

    override fun onStart() {
        super.onStart()
        viewModel.onStart(requireContext())
    }
}